from dishka import Provider, Scope, provide

from weed_shop.core.config import LoyverseSyncConfig
from weed_shop.infrastructure.database.services import OrderService, PaymentMethodService, ProductService, UserService
from weed_shop.infrastructure.loyverse.api_client.main import LoyverseClient
from weed_shop.infrastructure.loyverse.sync_manager.main import LoyverseSyncManager
from weed_shop.infrastructure.loyverse.sync_manager.sales_repo import SalesRepo


class LoyverseProvider(Provider):
    scope = Scope.REQUEST

    @provide(scope=Scope.APP)
    def get_loyverse_client(self, config: LoyverseSyncConfig) -> LoyverseClient:
        return LoyverseClient(
            auth_token=config.auth_token, store_id=config.loyverse_store_id, logfile_path=config.logfile_path
        )

    @provide
    def get_sales_repo(self, config: LoyverseSyncConfig) -> SalesRepo:
        return SalesRepo(config.db_url)

    @provide
    def get_sync_manager(
        self,
        config: LoyverseSyncConfig,
        loyverse_client: LoyverseClient,
        sales_repo: SalesRepo,
        product_service: ProductService,
        order_service: OrderService,
        payment_method_service: PaymentMethodService,
        user_service: UserService,
    ) -> LoyverseSyncManager:
        return LoyverseSyncManager(
            bot_store_id=config.bot_store_id,
            loyverse_store_id=config.loyverse_store_id,
            loyverse_client=loyverse_client,
            sales_repo=sales_repo,
            product_service=product_service,
            order_service=order_service,
            payment_method_service=payment_method_service,
            user_service=user_service,
            logfile_path=config.logfile_path,
        )
